/*----------------------------------------
	1.1.5) 数据库管理
	----------------------------------------*/
	var Admin;
	if (!Admin) Admin = {};
	Admin.dm = {};

	/**
	 * main gui of database manage
	 */
	Admin.dm.main = function()
	{
		$(".content_block").spin();
		$.ajax({
			url: dm_path + "main",
			success: function(html) {
				$(".content_block").spin(false);
				$(".content_block").html(html);
			}
		});
	}
	
	/**
	 * optimize table
	 * @param table_name
	 */
	Admin.dm.optimize = function(table_name)
	{
		$(".content_block").spin();
		$.ajax({
			url: dm_path + "table_optimize",
			dataType: 'json',
			data: {table_name: table_name},
			success: function(data) {
				$(".content_block").spin(false);
				$(".message").remove();
				if (data == 'optimize_succeeded') {
					$("caption").append(
						"<div class='message success'>" +
		                "<p>数据表<strong> " + table_name + " </strong>优化成功</p>" +
		                "</div>"
					);
				} else if (data == 'optimize_failed') {
					$("caption").append(
						"<div class='message error'>" +
		                "<p>数据表<strong> " + table_name + " </strong>优化失败</p>" +
		                "</div>"
					);
				}
				
			}
		});
	}
	
	/**
	 * repair table
	 * @param table_name
	 */
	Admin.dm.repair = function(table_name)
	{
		$(".content_block").spin();
		$.ajax({
			url: dm_path + "table_repair",
			dataType: 'json',
			data: {table_name: table_name},
			success: function(data) {
				$(".content_block").spin(false);
				$(".message").remove();
				if (data == 'repair_succeeded') {
					$("caption").append(
						"<div class='message success'>" +
		                "<p>数据表<strong> " + table_name + " </strong>修复成功</p>" +
		                "</div>"
					);
				} else if (data == 'repair_failed') {
					$("caption").append(
						"<div class='message error'>" +
		                "<p>数据表<strong> " + table_name + " </strong>修复失败</p>" +
		                "</div>"
					);
				}
			}
		});
	}
	
	/**
	 * submit backup database
	 */
	Admin.dm.submit_backup = function()
	{
		$(".content_block").spin();
		$.ajax({
			url: dm_path + "database_backup",
			dataType: 'json',
			success: function(data) {
				$(".content_block").spin(false);
				$(".message").remove();
				if (data == 'backup_succeeded') {
					$("caption").append(
						"<div class='message success'>" +
		                "<p><strong> 数据库 </strong>备份成功</p>" +
		                "</div>"
					);
				} else if (data == 'backup_failed') {
					$("caption").append(
						"<div class='message error'>" +
		                "<p><strong> 数据库 </strong>备份失败</p>" +
		                "</div>"
					);
				}
			}
		});
	}
	
	/**
	 * list restore files
	 */
	Admin.dm.list_restore = function()
	{
		$(".content_block").spin();
		$.ajax({
			url: dm_path + "restore_list",
			success: function(html) {
				$(".content_block").spin(false);
				$(".content_block").html(html);
			}
		});
	}
	
	/**
	 * delete restore file
	 */
	Admin.dm.delete_restore = function(backupdata)
	{
		$(".content_block").spin();
		$.ajax({
			url: dm_path + "backupdata_delete",
			dataType: 'json',
			data: {backupdata: backupdata},
			success: function(data) {
				$(".content_block").spin(false);
				if (data == 'delete_succeeded') {
					Admin.dm.list_restore();
				}
			}
		});
	}
	
	// listen to 'optimize' button
	$(".table_optimize").live('click', function() {
		var table_name = $(this).attr("id");
		Admin.dm.optimize(table_name);
		setTimeout("$('.message').slideUp(200)", 3000);
	});
	
	// listen to 'repair' buton
	$(".table_repair").live('click', function() {
		var table_name = $(this).attr("id");
		Admin.dm.repair(table_name);
		setTimeout("$('.message').slideUp(200)", 3000);
	});
	
	// listen to 'submit backup' button
	$("#database_backup_submit").live('click', function() {
		Admin.dm.submit_backup();
		setTimeout("$('.message').slideUp(200)", 3000);
	});
	
	// listen to 'to restore' button
	$("#database_restore").live('click', function() {
		Admin.dm.list_restore();
	});
	
	// listen to 'download restore' button
	$(".backupdata_download").live('click', function() {
		var backupdata = $(this).attr('id');
		$(".content_block").spin();
		$.ajax({
			url: dm_path + "backupdata_download",
			dataType: 'json',
			data: {backupdata: backupdata},
			success: function(data) {
				$(".content_block").spin(false);
			}
		});
	});
	
	// listen to 'delete restore' button
	$(".backupdata_delete").live('click', function() {
		var backupdata = $(this).attr('id');
		if (confirm('确认删除该备份？')) {
			Admin.dm.delete_restore(backupdata);
		} else {
			return false;
		}
	});
	
	